package com.salereport.web.service;

import com.salereport.web.vo.ReportVO;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

@Repository
public class ReportService {

    public List<ReportVO> read(HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("/");
        List<ReportVO> infos = null;
        try {
            FileInputStream fs = new FileInputStream(path+"/reprot_reserved.txt");//("foo.ser");
            ObjectInputStream ois = new ObjectInputStream(fs);
            infos = (List<ReportVO>) ois.readObject();
            ois.close();
        }catch (FileNotFoundException f){

        }catch (Exception ex) {
            ex.printStackTrace();
        }
        return infos;
    }

    public void write(HttpServletRequest request,List<ReportVO> infos){
        try {
            String path = request.getSession().getServletContext().getRealPath("/");
            FileOutputStream fs = new FileOutputStream(path+"/reprot_reserved.txt");
            ObjectOutputStream os = new ObjectOutputStream(fs);
            os.writeObject(infos);
            os.flush();
            os.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void clear(HttpServletRequest request){
        try {
            String path = request.getSession().getServletContext().getRealPath("/");
            File file = new File(path+"/reprot_reserved.txt");
            if(file.exists()){
                file.delete();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
